#!/bin/bash -e

function info() {
echo Usage: `basename $0` [pmtjn] cmd args
exit 2
}

while getopts  ":l:p:f:t:m:j:n:" opt; do
	case $opt in
	l) lines=$OPTARG;;
	p) cpu=$OPTARG;;
	m) mem=$OPTARG;;
	f) suffix=$OPTARG;;
	t) max_task=$OPTARG;;
	j) job_id=$OPTARG;;
	n) name=$OPTARG;;
	?) info;;
	esac
done
shift $(($OPTIND - 1))

# echo $@

if [ $# -lt 1 ]; then info; fi

. $var

if test -z "$max_task"; then max_task=20; fi
if test -z "$name"; then name=tes; fi
wait_time=60

while true; do
	if test $(qstat|grep `whoami`|wc -l) -lt $max_task; then
		sleep 2
		if test -n "$job_id"; then
			if test -n "$(sub -d$job_id -p$cpu -m$mem -n$name $@)"; then
				exec 1>&$log
				echo sub with dependency $job_id
			else
				sub -p$cpu -m$mem -n$name $@
				exec 1>&$log
				echo sub without dependency
			fi
		else
			echo sub without dependency
			
			sub -p$cpu -m$mem -n$name $@

		fi
		
		break
	else
		sleep $wait_time
	fi
done

# . $cmd_done